//
//  SignalRClientConnect.m
//  Coin
//
//  Created by yangy on 2018/8/27.
//  Copyright © 2018年 zzg. All rights reserved.
//

#import "SignalRClientConnect.h"


@interface SignalRClientConnect()
@property (strong, nonatomic, readwrite) SRHubProxy * hub;

@end

@implementation SignalRClientConnect
+ (instancetype)sharedConnection {
    static SignalRClientConnect *_sharedConnection = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedConnection = [[SignalRClientConnect alloc] initWithURLString:SRConnectionBaseURLString];
    });
    return _sharedConnection;
}

- (instancetype)initWithURLString:(NSString *)url {
    self = [super initWithURLString:url];
    if (!self) {
        return nil;
    }
    __weak __typeof(&*self)weakSelf = self;
    self.started = ^{
        __strong __typeof(&*weakSelf)strongSelf = weakSelf;
        NSLog(@"SignalRClientConnect successful");
        [strongSelf ping];
        //[strongSelf->_hub invoke:@"Init" withArgs:@[@"ETH_USDT",@"MIN1",@(1534662204000),@(1534748664000)]];
    };
    self.error = ^(NSError *error) {
        NSLog(@"SignalRClientConnect error:%@",error);
    };
    self.closed = ^{
        NSLog(@"SignalRClientConnect close");
    };
    
    _hub = [self createHubProxy:@"exchangeHub"];
    [_hub on:@"ReceiveFullMarket" perform:self selector:@selector(handleReceiveFullMarket:)];
    [_hub on:@"UpdateAsset" perform:self selector:@selector(handleUpdateAsset:)];
    [_hub on:@"ReceiveMarket" perform:self selector:@selector(handleReceiveMarket:)];
    [_hub on:@"ReceiveKline" perform:self selector:@selector(handleReceiveKline:)];
    return self;
}

- (void)initWithMarket:(NSString *)market interval:(NSInteger)interval start:(NSTimeInterval)start end:(NSTimeInterval)end
{
    //MIN1 MIN5 MIN15 MIN30 H1 D1
    NSString *timeStr = interval == 0? @"MIN1" : interval == 1? @"MIN5" : interval == 2? @"MIN15" : interval == 3? @"MIN30" : interval == 4 ? @"H1" : @"D1";
    [_hub invoke:@"Init" withArgs:@[kStringIsEmpty(market)? @"" : market,timeStr,@(start),@(end)]];
}

- (void)ping{
    [_hub invoke:@"Ping" withArgs:@[]];
}

- (void)registWithToken:(NSString *)token{
    [_hub invoke:@"Register" withArgs:@[token]];
}

- (void) handleReceiveFullMarket:(NSDictionary*) dict
{
    NSMutableArray *temp = [NSMutableArray array];
    NSArray *array = [dict objectForKey:@"Data"];
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MarketObject *market = [MarketObject yy_modelWithDictionary:obj];
        [temp addObject:market];
    }];
    
    if(self.ReceiveFullMarket){
        self.ReceiveFullMarket([temp copy]);
    }
    [kNotificationCenter postNotificationName:kReceiveFullMarketNotification object:[temp copy]];
}
- (void) handleUpdateAsset:(NSString*) message
{
    if(self.UpdateAsset){
        self.UpdateAsset(message);
    }
}
- (void) handleReceiveMarket:(NSDictionary*) dict
{
    ReceiveMarketObject *object = [ReceiveMarketObject yy_modelWithDictionary:[dict objectForKey:@"Data"]];
    if(self.ReceiveMarket){
        self.ReceiveMarket(object);
    }
    [kNotificationCenter postNotificationName:kReceiveMarketNotification object:object];
}
- (void) handleReceiveKline:(NSDictionary*) message
{
   
    if(kDictIsEmpty(message))
        return;
    
    id klines = [message objectForKey:@"Data"];
    if([klines isKindOfClass:[NSDictionary class]]){
        klines = [KLineObject yy_modelWithDictionary:klines];
        if(self.ReceiveKline){
            self.ReceiveKline(klines);
        }
    }else if([klines isKindOfClass:[NSArray class]]){
        NSMutableArray *temp = [[NSMutableArray alloc] init];
        [klines enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [temp addObject:[KLineObject yy_modelWithDictionary:obj]];
        }];
        if(self.ReceiveKline){
            self.ReceiveKline([temp copy]);
        }
    }
}
@end
